#!/usr/bin/env python
# encoding:utf-8

import xlwt
#读取文件的指定行并写入新文件
num = 0
l = []
file = open('d:/abc.txt','r')
# for line in file.readlines()[0:5]:  #选择读取0到5行
#将表格数据转换为列表
for line in file.readlines():
    l.append(line)
    num +=1
file.close()
print num  ,len(l)
#
# #将列表数据每次取出5000存到对应的文本文档里
# for y in range(0,(len(l)/5000)+1):
#     file1 = open('d:/%d.txt'%y, 'a+')
#     for a in l[y*5000:(y+1)*5000]:
#         file1.write(a)
#     file1.close()
#     print '%d导出成功'%y


# 将列表数据每次取出5000存到对应的表格里

x = 0
for y in range(0, (len(l) / 5000) + 1):
    workbook = xlwt.Workbook(encoding='ascii')  # 设置表格字符集
    worksheet = workbook.add_sheet('data')  # 设置工作簿名称
   # file1 = open('d:/%d.txt' % y, 'a+')
    for a in l[y * 5000:(y + 1) * 5000]:
        worksheet.write(x,0,a)
        x = x+1
    workbook.save('d:/%d.xls'%y)  # 将表格内容保存在本地
    print '%d导出成功' % y
    x = 0




# file1 = open('d:/1-5000.txt','a+')
# for a in l[0:5000]:
#     file1.write(a)
# file1.close()
# print '1-5000导出成功'





# file = open('d:/abc.txt', 'r')  #速度比较慢
# for line in file.readlines():
#     if num < 5000:
#         file_1 = open('f:/1.txt', 'a+')
#         file_1.write(line)
#         file_1.close()
#     elif num >= 5000 and num < 10000:
#         file_2 = open('f:/2.txt' , 'a+')
#         file_2.write(line)
#         file_2.close()
#     num = num +1
# file.close()